<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: NewsletterPortlet.php 461 2011-01-16 18:04:56Z steen.rabol $
 *
 *
 */

Prado::using('Application.Portlets.Portlet');
Prado::using('Application.Portlets.NewsletterPortlet.Common.NewsletterPortletRecord');
Prado::using('Application.Portlets.NewsletterPortlet.Common.NewsletterUserPortletRecord');

class NewsletterPortlet extends Portlet
{
	public function  onInit($p)
	{
		parent::onInit($p);
	}

	public function  onLoad($param)
	{
		parent::onLoad($param);
	}

	public function checkEmail($sender,$param)
	{
		$nlUser = NewsletterUserPortletRecord::finder()->findByemail($this->NewsletterEmail->Text);
		PortalUtil::Info($nlUser);
		$param->IsValid = is_null($nlUser) ? true : false;
	}

	public function AddRemoveEmail($sender,$param)
	{
		if($this->Page->IsPostBack && $this->Page->IsValid)
		{
			$nlUser	= NewsletterUserPortletRecord::finder()->findByemail($this->NewsletterEmail->Text);
			if($nlUser)
			{
				$nlUser->delete();
				$msg = Prado::localize("Email removed from newsletter");
			}
			else
			{
				$nlUser	= new NewsletterUserPortletRecord();
				$nlUser->email	= $this->NewsletterEmail->Text;
				$nlUser->save();
				$msg	= Prado::localize("Email added to newsletter list");
			}

			$this->pnlNewsletterEnter->Enabled	= false;
			$this->pnlNewsletterEnter->Visible	= false;

			$this->pnlNewsletterResult->Enabled	= true;
			$this->pnlNewsletterResult->Visible	= true;
			$this->nlResult->Text = $msg;
		}



	}


}
?>